//COPYRIGHT AND PERMISSION NOTICE
//
//Copyright (c) 1999,2000,2001 Brian Bennewitz
//
//All rights reserved.
//
//Permission is hereby granted, free of charge, to any person obtaining a
//copy of this software and associated documentation files (the
//"Software"), to deal in the Software without restriction, including
//without limitation the rights to use, copy, modify, merge, publish,
//distribute, and/or sell copies of the Software, and to permit persons
//to whom the Software is furnished to do so, provided that the above
//copyright notice(s) and this permission notice appear in all copies of
//the Software and that both the above copyright notice(s) and this
//permission notice appear in supporting documentation.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
//OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
//HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
//INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
//FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
//NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
//WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
//Except as contained in this notice, the name of a copyright holder
//shall not be used in advertising or otherwise to promote the sale, use
//or other dealings in this Software without prior written authorization
//of the copyright holder.
#pragma once
#define _CRT_SECURE_NO_DEPRECATE

#include "stdafx.h"
//#include <io.h>
//#include <fcntl.h>
//#include <sys/stat.h>
//#include <direct.h>
//#include <string.h>
//#include <stdio.h>
//#include <commctrl.h>
//#include <objbase.h>
//#include <shlobj.h>

#include "resource.h"

//#include <fstream>
//#include <string>
//#include "bvect.h"
#include "translhextion.h"
#include "japanese.h"
#include "constants.h"
#include "structs.h"
#include "globals.h"
#include "hex.h"
#include "progress.h"
#include "strutil.h"
#include "table.h"
#include "bytecode.h"
#include "Menu.h"
#include "HexFrame.h"

#include "filevect.cpp"
#include "search.h"

#define MAX_LOADSTRING 100

class Translhextion : public CWnd
{
public:
	Translhextion();
	~Translhextion();
	void Create();
	virtual LRESULT OnMessage(UINT iMsg, WPARAM wParam, LPARAM lParam);
	virtual UINT OnCommand(WPARAM wParam, LPARAM lParam);
	
	int iWindowShowCmd;
	int iWindowX;
	int iWindowY;
	int iWindowWidth;
	int iWindowHeight;
	int	iCurrentLine;
	long iCurrentByte;
	long iCurrentNibble;
	//MESSAGE PROCESSING
	bool CreateToolbar();
	void EnableToolbarButton(UINT uID, bool enable);
	int dropfiles( HANDLE hDrop );
	VOID MakeChartMenu();
	VOID MakeLineMenu(HPEN *phpen, HBITMAP *phbmp);
	//HELPER FUNCTIONS
	inline char TranslateAnsiToOem (TCHAR c);
	void draw_border_client_rect (HDC hdc);
	void destroy_backbuffer ();
	void adjust_view_for_selection ();
	void update_size_change ();
	int wread_table_file(LPCTSTR filename);
	void adjust_view_for_cursor ();
	void print_line( HDC hdc, int iLine, TCHAR* linebuffer, HBRUSH hbr );
	void invert_char (HDC hdc);
	void thingy_view_invert_char (HDC hdc, int iLine);
	void update_horizontal_scrollbar ();
	void update_vertical_scrollbar ();
	void clear_all ();
	void update_window_status ();
	void place_cursor ();
	int timer (WPARAM w, LPARAM l);
	int make_font ();
	int make_jfont ();
	int close ();
	int translate_text_to_binary( _tstring& in, FileVector<char>& out );
	int	translate_binary_to_text( _tstring& dest, char* src, int len );
	int find_byte_pos (TCHAR* src, TCHAR c);
	int find_and_select_data( _tstring& finddata, int finddir, int do_repaint, int (*cmp) (u8*));
	int replace_selected_data( _tstring& replacedata, int do_repaint = TRUE );
	int save_ini_data ();
	int read_ini_data ();
	//void reg_set_string(HKEY &hkeyKEY, LPCTSTR name, LPCTSTR string);
	int get_diffs (char* ps, int sl, char* pd, int dl, intpair* pdiff);
	int compare_arrays (char* ps, int sl, char* pd, int dl);
	int initmenupopup (WPARAM w, LPARAM l);
	int repaint (int iLine=-1);
	int load_file (LPCTSTR fFileName);
	int file_can_load (LPCTSTR fFileName);
	int on_window_init (HWND hw, HINSTANCE hI);
	int resize_window (int cx, int cy);
	int set_focus ();
	int kill_focus ();
	int lbuttondown (int xPos, int yPos);
	int keydown (int key);
	int character (char ch);
	int vertical_scroll (int cmd, int pos);
	int horizontal_scroll (int cmd, int pos);
	int paint ();
	void DrawFrame(HDC lpDC);
	int destroy_window ();
	_tstring create_tablefind_string(_tstring x);
	//BYTECODE
	//int byte_to_bytecode_destinationlength (char* src, int iLengthgthOfSource);
	//int translate_bytes_to_bytecode (TCHAR* pd, unsigned char* src, int iLengthgthOfSource);
	//int is_bytecode (TCHAR* src, int len);
	//int calc_bytecode_translation_destinationlength (TCHAR* src, int iLengthgthOfSource);
	//int translate_bytecode (u8* dest, TCHAR* src, int iLengthgthOfSource, int binmode=LITTLE_ENDIAN);
	//int bytecode_translate (u8* dest, TCHAR* src, int iLengthgthOfSource, int charmode=ANSI_FIXED_FONT, int binmode=LITTLE_ENDIAN);
	//int bytecode_translate (u8** ppd, TCHAR* src, int iLengthgthOfSource, int charmode=ANSI_FIXED_FONT, int binmode=LITTLE_ENDIAN);
	//LISTS
	int make_most_recent_list (HMENU menu);
	int update_most_recent ();
	int make_most_recent_table_list (HMENU menu);
	int update_most_recent_table ();
	int make_bookmark_list (HMENU menu);
	int make_scriptmark_list (HMENU menu);
	//TEMPLATES
	int ignore_non_code( char* pcTpl, int tpl_len, int& index );
	int read_tpl_token( char* pcTpl, int tpl_len, int& index, char* name );
	int apply_template_on_memory( char* pcTpl, int tpl_len, FileVector<char>& ResultArray );
	int apply_template(LPCTSTR pcTemplate );
	//COMMAND
	int mousemove (int xPos, int yPos);
	int lbuttonup (int xPos, int yPos);
	int rbuttonup (int xPos, int yPos);
	int on_backspace ();
	int on_deletekey ();
	int select_with_arrowkeys (int key);
	//FILE
	int _New ();
	int open ();
	int save ();
	int save_as ();
	int open_partially ();
	void revert();
	void deletefile();
	int properties ();
	int apply_template();
	int summon_text_edit();
	int most_recent_selection (int cmd);
	//SELECTION
	int edit_cut ();
	int edit_copy ();
	int thingy_copy ();
	int fast_paste ();
	int edit_paste ();
	int select_all ();
	int select_block ();
	void fill_with();
	int copy_hexdump ();
	void save_selection_as();
	//OFFSET
	int _Goto();
	int manipulate_bits ();
	int compare ();
	//INSERT
	int toggle_insertmode ();
	int edit_enterdecimalvalue ();
	int edit_append ();
	void insertfile();
	//SEARCH
	int find ();
	int find_hex ();
	int find_with_table();
	int findnext();
	int findnext_relative();
	int findprev();
	int findprev_relative();
	void replace();
	void replace_with_table();
	relativescandata guess_table_values(long start);
	int search_relative();
	void save_relative_results(fstream& fs);
	long scan_relative_helper(int iLengthgthOfSource);
	int scan_relative();
	int value_scan_relative();
	//BOOKMARK
	int add_bookmark ();
	int remove_bookmark ();
	int clear_all_bookmark ();
	int goto_bookmark (int cmd);
	//SCRIPT
	int open_table();
	int most_recent_table_selection (int cmd);
	int convert_table();
	int script_dump ();
	int replace_script ();
	void optimize_script_file();
	//OPTIONS
	int view_settings ();
	int color_settings ();
	int binarymode ();
	int offsetmode ();
	//SCROLL
	//HELP
	int iGetStartOfSelection()
	{
		if( iStartOfSelection < iEndOfSelection )
			return iStartOfSelection;
		else
			return iEndOfSelection;
	}

	int iGetEndOfSelection()
	{
		if( iStartOfSelection < iEndOfSelection )
			return iEndOfSelection;
		else
			return iStartOfSelection;
	}

private:
	int bDontMarkCurrentPos;
	int bInsertingHex;
	int iHexWidth;
	int bReadOnly;
	int bOpenReadOnly;
	int iPartialOffset;
	int bPartialOpen;
	int iBookmarkCount;
	int iMostRecentCount;
	int iMostRecentTableCount;
	int bFilestatusChanged;
	int iUpdateLine;
	int bTimerSet;
	int iMouseX;
	int iMouseY;
public:
	int iBinaryMode;
private:
	int bUnsignedView;
	int iFontSize;
	int iInsertMode;
public:
	int iCharacterSet;
private:
	int iTextColor;
	int iBkColor;
	int iTextColorE;
	int iTextColorO;
	int bFileNeverSaved;
	int bSelected;
	int bLButtonIsDown;
	int iLBDownX;
	int iLBDownY;
	int iStartOfSelection;
	int iEndOfSelection;
	int iTextEntryMode;
	int iFileChanged;
	int cCharXDim;
	int cxCaps;
	int cCharYDim;
	int cClientYPos;
	int cClientXDim;
	int cClientYDim;
	int cxBuffer;
	int cBufferY;
	int iNumlines;
	int iVerticalScrollMax;
	int iVerticalScrollPos;
	int iVerticalScrollInc;
	int	iHorizontalScrollMax;
	int iHorizontalScrollPos;
	int iHorizontalScrollInc;
	int iOffsetLength;
	int iByteSpace;
	int iCharSpace;
	int iCharsPerLine;
	GString strMostRecent[iMostRecentMaximum];
	GString strMostRecentTable[iMostRecentTableMaximum];
	GString filename;	// opened file
	TCHAR *filename2;	// opened table
	bookmark pbmkList[iBookmarkMax];
	HFONT hFont;
	COLORREF iDTEColorValue;
	COLORREF iChangesColorValue;
	COLORREF iHighlightColorValue;
	COLORREF iTextColorEValue;
	COLORREF iTextColorOValue;
	COLORREF iTextColorValue;
	COLORREF iBkColorValue;
	COLORREF iSepColorValue;
	COLORREF iSelBkColorValue;
	COLORREF iSelBkColor;
	COLORREF iSelTextColorValue;
	COLORREF iSelTextColor;
	COLORREF iBookmarkColor;
	_tstring sTexteditorName;
	FileVector<TCHAR> Linebuffer;
	FileVector<unsigned char> FileBuffer;
	HWND hwndStatusBar, /*hwndHBar, hwndVBar,*/ hwndTBar, hToolbar;
	HINSTANCE hInstance;

	GString xml_name;
public:
	CTable table;
	CSearch src_helper;
	CMenu m_Menu;
	CHexFrame m_Frame;
};
